package ar.uba.fi.RSACryptoFiuba.model.decrypt;

import ar.uba.fi.RSACryptoFiuba.model.InvalidValueException;
import ar.uba.fi.RSACryptoFiuba.model.RSACalculator;
import ar.uba.fi.RSACryptoFiuba.model.RSAMessage;
import ar.uba.fi.RSACryptoFiuba.model.RSAStep;

public class ConvertNumericalRepresentationStep extends RSAStep {

	@Override
	public void execute(RSAMessage message) {
		RSADecryptMessage decryptMessage = (RSADecryptMessage) message;
		String plainText = RSACalculator.getTextFromASCII(decryptMessage.getNumericalRepresentation());
		decryptMessage.setPlainText(plainText);
		
		decryptMessage.update();
	}

	@Override
	public void undo(RSAMessage message) {
		RSADecryptMessage decryptMessage = (RSADecryptMessage) message;
		decryptMessage.setPlainText("");
		
		decryptMessage.update();
	}

	@Override
	public void test(RSAMessage message) {
		RSADecryptMessage decryptMessage = (RSADecryptMessage) message;
		String element = decryptMessage.getElementToEvaluate();
		String plainText = RSACalculator.getTextFromASCII(decryptMessage.getNumericalRepresentation());
		boolean isValid = plainText.equals(element);
		if(!isValid){
			throw new InvalidValueException("El valor debe ser el texto correspondiente al valor ASCII de la representación numérica.");
		}
		decryptMessage.setPlainText(element);
		
		decryptMessage.update();
	}

}
